﻿Public Class fNewRec

    Private Sub fNewRec_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.Dispose()
    End Sub

    Private Sub fNewRec_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'HhdtbDataSet1.t_weight' table. You can move, or remove it, as needed.
        Me.T_weightTableAdapter.Fill(Me.HhdtbDataSet1.t_weight)

        'add tabs to tabControl according to selected items in Settings/Basic
        Dim vals() As String = My.Settings.setBasic.Trim.Split(";")
        Dim tree As TreeView = My.Forms.fSetBasic.trVSel
        Dim c As TabControl = Me.tabContr

        Try
            For i As Integer = 0 To vals.GetUpperBound(0)
                If vals(i) = "1" Then
                    'create tabs and name them
                    Dim name As String = tree.Nodes(i).Text
                    Dim page As New TabPage()
                    With page
                        .Name = name
                        .Text = name
                    End With
                    c.TabPages.Add(page)
                    c.Update()
                    'create controls and other items in tabs
                    'controls for adding weight record
                    Try
                        If name = "Weight" Then
                            Dim lbl As New Label() 'add new label
                            With lbl
                                .Name = "lblWeight"
                                .Text = "Your current weight:"
                                .Size = New Size(120, 30)
                                .AutoSize = True
                                .Location = New Point(10, 20)
                                .Anchor = AnchorStyles.Top Or AnchorStyles.Left
                                .Visible = True
                                .Enabled = True
                            End With
                            c.TabPages(i).Controls.Add(lbl)

                            Dim num As New NumericUpDown 'add new numeric control
                            With num
                                .Name = "numWeight"
                                .Size = New Size(120, 30)
                                .AutoSize = True
                                .Location = New Point(lbl.Location.X + 125, lbl.Location.Y)
                                .Anchor = AnchorStyles.Left Or AnchorStyles.Top
                                .Visible = True
                                .Enabled = True
                                .DecimalPlaces = 2
                                .Maximum = 1000
                                .Minimum = 0
                                .TextAlign = HorizontalAlignment.Center
                            End With
                            c.TabPages(i).Controls.Add(num)

                            Dim lblDate As New Label() 'add new label for date picker
                            With lblDate
                                .Name = "lbl_dpWeight"
                                .Text = "Date of weight-in:"
                                .AutoSize = True
                                .Size = New Size(120, 30)
                                .Location = New Point(lbl.Location.X, lbl.Location.Y + 25)
                                .Visible = True
                                .Enabled = True
                            End With
                            c.TabPages(i).Controls.Add(lblDate)

                            Dim dp As New DateTimePicker() 'add date picker
                            With dp
                                .Name = "dpWeight"
                                .Value = Now.Date
                                .Size = New Size(120, 30)
                                .Anchor = AnchorStyles.Top Or AnchorStyles.Left
                                .Location = New Point(num.Location.X, num.Location.Y + 25)
                                .Visible = True
                                .Enabled = True
                            End With
                            c.TabPages(i).Controls.Add(dp)

                            c.TabPages(i).Update()
                            'end creating weight tab
                        ElseIf name = "Physical Activities" Then

                            'end creating PA tab
                        ElseIf name = "Calories Intake" Then


                            'end creating Calories tab
                        End If

                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Critical, "Error generating tabs")
                        Exit Sub
                    End Try
                    c.Update()

                End If
            Next i
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error generating tabs")
            Exit Sub
        End Try
        c.Show()
    End Sub

    Private Sub btnOK_Click(sender As System.Object, e As System.EventArgs) Handles btnOK.Click
        Try

            For i As Integer = 0 To Me.tabContr.TabPages.Count() - 1
                Dim cntrl As TabPage.TabPageControlCollection = Me.tabContr.TabPages(i).Controls
                Dim rid As Long = 0

                If Me.tabContr.TabPages.Item(i).Name = "Weight" Then 'save "weight" tab into dtb
                    Try
                        Dim num As NumericUpDown = cntrl.Item("numWeight")
                        Dim d As Decimal = num.Value
                        Dim p As DateTimePicker = cntrl.Item("dpWeight")
                        Dim dv As Date = p.Value
                        Dim t As DataTable = Me.HhdtbDataSet1.t_weight
                        Dim tc As Long = t.Rows.Count()

                        Dim r As DataRow = t.NewRow()
                        For j As Long = 0 To tc - 1
                            If t.Rows.Item(j).Item(0) > rid Then
                                rid = t.Rows.Item(j).Item(0)
                            End If
                        Next j
                        r(0) = rid + 1
                        r(1) = d
                        r(2) = dv
                        t.Rows.Add(r)
                        Me.T_weightTableAdapter.Update(t)

                    Catch exc As Exception
                        MsgBox(exc.Message, MsgBoxStyle.Critical, "Error saving to dtb, table 'Weight'")
                        Exit For
                        Exit Sub
                    End Try

                ElseIf Me.tabContr.TabPages.Item(i).Name = "Physical Activities" Then 'save PA tab into dtb



                ElseIf Me.tabContr.TabPages.Item(i).Name = "Calories Intake" Then 'save Calories tab into dtb

                End If
            Next i
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
            Exit Sub
        End Try

        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub btnCancel_Click(sender As System.Object, e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub


End Class